var gen="http://localhost:8080/zyx";
/*显示默认月份的日历*/
let curTime = new Date();
//当前小时
let time = curTime.getHours();
//当年
let curYear = curTime.getFullYear()
//当月
let curMonth = curTime.getMonth()+1;
//显示三个月
var yearHtml1 = `<a>`+curYear+`年`+curMonth+`月</a>`;
var curYear2=curYear;
var curMonth2=(curMonth+1)%13;
if (curMonth2===0){curMonth2=1;}
if (curMonth2===1){curYear2=curYear+1;}
var yearHtml2 = `<a>`+curYear2+`年`+curMonth2+`月</a>`;
var curYear3=curYear;
var curMonth3=(curMonth+2)%13;
if (curMonth3===0){curMonth3=1;}
if (curMonth3===1){curYear3=curYear+1;}
var yearHtml3 = `<a>`+curYear3+`年`+curMonth3+`月</a>`;
var dayTicketPrice = $(".info-price");

dayTicketPrice = dayTicketPrice[0].children[1].innerText;
dayTicketPrice = dayTicketPrice.substr(0,3)
console.log("dayTicketPrice:-----------",dayTicketPrice);
var weekDayTicketPrice = Number(dayTicketPrice)+dayTicketPrice/10;
weekDayTicketPrice = weekDayTicketPrice.toFixed(0)
//当天
curDate = curTime.getDate();
console.log(curYear)
console.log(curMonth)
$(".month-tab-9").html(yearHtml1);
$(".month-tab-10").html(yearHtml2);
$(".month-tab-11").html(yearHtml3);
render(curYear, curMonth);
// 判断平年还是闰年
function isLeapYear(year) {
    return (year % 400 === 0) || ((year % 4 === 0) && (year % 100 !== 0))
}
// 景点id
var sId;
// 日历
function render(curyear, curmonth) {
    sId = parseInt($(".sales-title")[0].children[0].id);
    curmonth-=1;
    $(".day").html("");
    let daysInMonth = [31, isLeapYear(curyear) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    // 确定今天日期所在月的第一天是星期几
    let firstDayInMonth = new Date(curyear, curmonth,0);
    //
    let firstDayWeek = firstDayInMonth.getDay();
    // 几行
    let calendarRows = Math.ceil((firstDayWeek + daysInMonth[curmonth])/7);

    let t=1;
    // 外循环渲染日历的每一行
    for (let i = 0; i < calendarRows; i++) {
        var html="";
        for (let j = 0; j < 7; j++) {
            let idx = i * 7 + j;
            let date = idx - firstDayWeek + 1;
            if (date <= 0 || date > daysInMonth[curmonth]) {
                html += `<li></li>`
            }else if(tickets(curyear, curmonth,(t))===0 || curmonth===curMonth-1 && t<curDate || (curDate===t && time>17)){
                html += `<li style="color: #999;line-height: 48px">`+t+`<div class="day-price"><span style="margin: -15px 0 0 0;display: block;">售空</span></div></li>`;
                t++;
            }else if(j===0 || j===6){
                html += `<li class="residual">
                                <div class="border"></div>
                                <div class="day-number">`+(t++)+`</div>
                                <div class="day-price">
                                    <span style="margin:0 -6px 0 0">￥</span>
                                    <span>`+weekDayTicketPrice+`</span>
                                    <span style="margin:0 0 0 -3px">起</span>
                                    <br>
                                    <span>余</span>
                                    <span>`+tickets(curyear, curmonth,(t-1))+`</span>
                                </div>
                            </li>`
            }else{
                html += `<li class="residual">
                                <div class="border"></div>
                                <div class="day-number">`+(t++)+`</div>
                                <div class="day-price">
                                    <span style="margin:0 -6px 0 0">￥</span>
                                    <span>`+dayTicketPrice+`</span>
                                    <span style="margin:0 0 0 -3px">起</span>
                                </div>
                                <div class="day-price">
                                    <span>余</span>
                                    <span>`+tickets(curyear, curmonth,(t-1))+`</span>
                                </div>
                            </li>`
            }
        }
        $(".day").append(html);
    }
}
var ticketsNum=0;

console.log(sId)
function tickets(year,month,day){
    var tik=0;
    var date = new Date(year,month,day);
    var url = gen+"/date/tickets/"+sId;
    $.ajax({
        url:url,
        data:{"date":date},
        type:"GET",
        async:false,
        success:function (result){
            if(result.code===200){
                ticketsNum=0;
                for (let i = 0; i < result.data.length; i++) {
                    ticketsNum+=result.data[i];
                }
                tik=30-ticketsNum;
            }else {
                tik=30;
            }
        }
    })
    if(tik<0){
        return 0
    }
    return tik;
}

document.querySelector(".calendar-year-month").onclick = function (event){
    var ele = event.target;
    ele = ele.parentElement;
    /*月份点击*/
    if(ele.nodeName === "LI"){
        var liEle = ele.parentElement.children;
        for (let i = 0; i < liEle.length; i++) {
            liEle[i].classList.remove('month-tab');
        }
        ele.classList.add('month-tab');

        //点击选择月份
        if(ele.classList.contains("month-tab-9")){
            render(curYear,curMonth);
        }
        if (ele.classList.contains("month-tab-10")){
            render(curYear,curMonth+1);
        }
        if (ele.classList.contains("month-tab-11")){
            render(curYear,curMonth+2);
        }
    }
    /*左右按钮*/
    var ulEle = document.querySelector(".month-ul");
    if(ele.nodeName === "SPAN" && ele.classList.contains("left")){
        for (let i = 0; i < ulEle.children.length; i++) {
            if(ulEle.children[i].classList.contains("month-tab") && !ulEle.children[i].classList.contains("month-tab-9")){
                for (let j = 0; j < ulEle.children.length; j++) {
                    ulEle.children[j].classList.remove("month-tab");
                }
                ulEle.children[i].previousElementSibling.classList.add("month-tab");
                render(curYear,curMonth+i-1);
            }
        }
    }
    if(ele.nodeName === "SPAN" && ele.classList.contains("right")){
        for (let i = ulEle.children.length-1; i>=0; i--) {
            if(ulEle.children[i].classList.contains("month-tab") && !ulEle.children[i].classList.contains("month-tab-11")){
                for (let j =  ulEle.children.length-1; j>=0; j--) {
                    ulEle.children[j].classList.remove("month-tab");
                }
                ulEle.children[i].nextElementSibling.classList.add("month-tab");
                render(curYear,curMonth+i+1);
            }
        }
    }
}

document.querySelector(".day").onclick = function (event){
    var ele = event.target;
    if(ele.nodeName === "DIV" && ele.classList.contains('border')){
        var childrenEle = ele.parentElement.parentElement;
        for (var i = 0; i < childrenEle.children.length; i++) {
            if(childrenEle.children[i].classList.contains('residual')){
                childrenEle.children[i].children[0].classList.remove('day-bc');
            }
        }
        ele.classList.add('day-bc');
        for (let i = 0; i < childrenEle.children.length; i++) {
            if (childrenEle.children[i].classList.contains('residual')){
                childrenEle.children[i].children[1].style.color='#999';
                childrenEle.children[i].children[2].style.color='#38c4ff';
            }
        }
        ele.parentElement.children[1].style.color='white';
        ele.parentElement.children[2].style.color='white';

        var day = ele.nextElementSibling.innerText;

        var date = document.querySelector(".date");
        var month = document.querySelector(".month-ul").children;
        var monthText ="";
        for (let i = 0; i < month.length; i++) {
            if(month[i].classList.contains('month-tab')){
                monthText=month[i].children[0].innerText;
            }
        }

        var yearAndMonth = null;
        var year = monthText.substr(0,4);
        var month = null;
        if (monthText.length == 8){
            month = monthText.substr(5,2);
        }else {
            month = monthText.substr(5,1);
        }
        yearAndMonth = year + "-"  +month + "-";

        // date.children[1].children[0].innerText =monthText+day+"日";
        date.children[1].children[0].innerText =yearAndMonth+day;

        /*每天的价格*/
        var startPrice = ele.parentElement.children[2].children[1].innerText;
        document.querySelector(".fri-price").children[1].innerText=startPrice;
        allPrice();
    }
}

document.querySelector(".adult-amount").onclick = function (event){
    var ele = event.target;
    var number = document.querySelector(".input-adult .number").innerText;
    // var price;
    var dayEle = document.querySelector(".day");
    if(ele.nodeName==="BUTTON" && ele.innerText === "+"){
        var flag = false;
        for (var i = 0; i < dayEle.children.length; i++) {
            if (dayEle.children[i].classList.contains('residual')){
                if(dayEle.children[i].children[0].classList.contains('day-bc')){
                    flag=true;
                    price = parseFloat(dayEle.children[i].children[2].children[1].innerText);
                    break;
                }
            }
        }
        if(flag){
            document.querySelector(".input-adult .number").innerText=parseInt(number)+1;
            // var priceAdult = parseInt(document.querySelector(".input-adult .number").innerText);
            // document.querySelector(".fri-price").children[1].innerText=price*priceAdult;

        }else {
            alert("请先选择日期！");
        }
        allPrice();
    }
    if(ele.nodeName==="BUTTON" && ele.innerText === "-"){
        if(number>1){
            document.querySelector(".input-adult .number").innerText=parseInt(number)-1;
            // var priceAdult = parseInt(document.querySelector(".input-adult .number").innerText);
            // document.querySelector(".fri-price").children[1].innerText=price*priceAdult;
        }
        allPrice();
    }


}

document.querySelector(".child-amount").onclick = function (event){
    var ele = event.target;
    var number = document.querySelector(".input-child .number").innerText;
    if(ele.nodeName==="BUTTON" && ele.innerText === "+"){
        var flag = false;
        var dayEle = document.querySelector(".day");
        // var price;
        for (var i = 0; i < dayEle.children.length; i++) {
            if (dayEle.children[i].classList.contains('residual')){
                if(dayEle.children[i].children[0].classList.contains('day-bc')){
                    // price = parseFloat(dayEle.children[2].children[2].children[1].innerText);
                    flag=true;
                }
            }
        }
        if(flag){

            document.querySelector(".input-child .number").innerText=parseInt(number)+1;
            // var allPrice = parseFloat(document.querySelector(".fri-price").children[1].innerText);
            // var priceChild = parseInt(document.querySelector(".input-child .number").innerText);
            // document.querySelector(".fri-price").children[1].innerText=allPrice+price/2*priceChild;
        }else {
            alert("请先选择日期！");
        }
        // document.querySelector(".input-child .number").innerText=parseInt(number)+1;
        allPrice();
    }
    if(ele.nodeName==="BUTTON" && ele.innerText === "-"){
        if(number>0){
            document.querySelector(".input-child .number").innerText=parseInt(number)-1;
        }
        allPrice();
    }
}

function allPrice(){
    /*获得成人数量*/
    var adultNumber = parseInt(document.querySelector(".input-adult .number").innerText);
    /*获得儿童数量*/
    var childNumber = parseInt(document.querySelector(".input-child .number").innerText);

    var price;
    var dayEle = document.querySelector(".day");
    for (var i = 0; i < dayEle.children.length; i++) {
        if (dayEle.children[i].classList.contains('residual')){
            if(dayEle.children[i].children[0].classList.contains('day-bc')){
                price = parseFloat(dayEle.children[i].children[2].children[1].innerText);
                break;
            }
        }
    }
    document.querySelector(".fri-price").children[1].innerText = price*adultNumber+(price/2)*childNumber;


}

$(".purchase-btn").click(function (){
    var innerText = $(".price1")[0].innerText;

    if (innerText==="--"){
        alert("请选择日期！");
        window.event.returnValue=false
    }
})
review(1);
function review(pageNum){

    $(".review-item").html("");
    var sightsId = parseInt($(".sales-title")[0].children[0].id);

    var url =gen+ "/sales/sights/review/sightsReview/"+pageNum;
    $.get(
        url,
        {"sightsId":sightsId},
        function (result){
            console.log(result)
            if (result.code === 200) {
                for (let i = 0; i < result.data.list.length; i++) {
                    // console.log(timeStamp2String(result.data.list[i].sightsReview.sightsReviewTime.getTime()))
                    var time = new Date(result.data.list[i].sightsReview.sightsReviewTime);
                    var contentHtml =
                        `<li>
                            <div class="user-logo">
                                <img src="`+result.data.list[i].customer.custProfile+`">
                            </div>
                            <div class="user-name">
                                <span style="font-family: Arial;font-weight: bold">`+result.data.list[i].customer.custNickname+`</span>
                            </div>
                            <div class="review-content">
                                <p>`+result.data.list[i].sightsReview.sightsReviewText+`</p>
                                <div class="reviewTime">
                                   <p>`+time.toLocaleDateString()+' '+time.toTimeString().substr(0,8)+`</p>
                                </div>
                            </div>
                        </li>`
                    $(".review-item").append(contentHtml)
                }
                /*分页*/
                layui.use('laypage', function(){
                    var laypage = layui.laypage;
                    //执行一个laypage实例
                    laypage.render({
                        elem: 'page'
                        ,count: result.data.total //数据总数，从服务端得到
                        ,theme: '#38c4ff'
                        ,limit: result.data.pageSize//每页显示
                        ,curr: result.data.pageNum
                        ,jump:function (obj,first){
                            if (!first){
                                result.data.pageNum=obj.curr;
                                review(result.data.pageNum);
                            }
                        }
                    });
                });
                // var prePage = result.data.prePage;
                // var nextPage = result.data.nextPage;
                // var pageEle;
                // if (!result.data.hasPreviousPage) {
                //     pageEle =
                //         '<span>' + '共' + result.data.pages + '页/' + result.data.total + '条' + '</span>' +
                //         '<span>' + '当前是第' + result.data.pageNum + '页' + '</span>' +
                //         '<a href="javascript:review('  + nextPage + ')">下一页</a>'
                //     $(".page").html(pageEle);
                // } else if (!result.data.hasNextPage) {
                //     pageEle = '' +
                //         '<span>' + '共' + result.data.pages + '页/' + result.data.total + '条' + '</span>' +
                //         '<a href="javascript:review('  + prePage + ')">上一页</a>' +
                //         '<span>' + '当前是第' + result.data.pageNum + '页' + '</span>'
                //     $(".page").html(pageEle);
                // } else {
                //     pageEle = '' +
                //         '<span>' + '共' + result.data.pages + '页/' + result.data.total + '条' + '</span>' +
                //         '<a href="javascript:review('  + prePage + ')">上一页</a>' +
                //         '<span>' + '当前是第' + result.data.pageNum + '页' + '</span>' +
                //         '<a href="javascript:review('  + nextPage + ')">下一页</a>'
                //     $(".page").html(pageEle);
                // }
            }
        },
        "json"
    )
}

function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

$(".purchase-btn").click(function () {
    var date = $(".datetime").text();
    console.log("=====================================");
    console.log("date的类型:" + typeof (date));
    console.log(date);
    var day = $(".dateday").text();
    console.log("day的类型:" + typeof (day));
    console.log(day);

    var audltNum = $(".adultNumber").text();
    console.log(audltNum);
    console.log("audltNum的类型:" + typeof (audltNum));

    var childNum = $(".childNumber").text();
    console.log(childNum);

    var pricecost = $(".price1").text();
    console.log("pricecost:" + pricecost);



    sessionStorage.setItem("day", day);
    sessionStorage.setItem("date", date);
    sessionStorage.setItem("audltNum", audltNum);
    sessionStorage.setItem("childNum", childNum);
    sessionStorage.setItem("pricecost", pricecost);

});